package com.baizhi.service.impl;

import com.baizhi.entity.PmsCategory;
import com.baizhi.mapper.PmsCategoryMapper;
import com.baizhi.service.PmsCategoryService;
import com.baizhi.vo.AppCategoryVo;
import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Digits;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author yu
 */

@Service
public class PmsCategoryServiceImpl implements PmsCategoryService {
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;
    @Override
    public R showCategories (Long categoryId) {
        List<PmsCategory> list = pmsCategoryMapper.selectList(null);
        List<AppCategoryVo> collect = list.stream().filter(new Predicate<PmsCategory>() {
            @Override
            public boolean test(PmsCategory pmsCategory) {
                if (pmsCategory.getParentCid() == categoryId) {
                    return true;
                }
                return false;
            }
        }).map(new Function<PmsCategory, AppCategoryVo>() {
            @Override
            public AppCategoryVo apply(PmsCategory pmsCategory) {
                AppCategoryVo appCategoryVo = new AppCategoryVo();
                appCategoryVo.setId(pmsCategory.getCatId());
                appCategoryVo.setCategoryName(pmsCategory.getName());
                List<AppCategoryVo> collect1 = list.stream().filter(new Predicate<PmsCategory>() {
                    @Override
                    public boolean test(PmsCategory pmsCategory2) {
                        if (pmsCategory2.getParentCid() == pmsCategory.getCatId()) {
                            return true;
                        }
                        return false;
                    }
                }).map(new Function<PmsCategory, AppCategoryVo>() {
                    @Override
                    public AppCategoryVo apply(PmsCategory pmsCategory2) {
                        AppCategoryVo appCategoryVo1 = new AppCategoryVo();
                        appCategoryVo1.setId(pmsCategory2.getCatId());
                        appCategoryVo1.setCategoryName(pmsCategory2.getName());
                        return appCategoryVo1;
                    }
                }).collect(Collectors.toList());
                appCategoryVo.setChildren(collect1);
                return appCategoryVo;
            }
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }
}
